function reverse(arr) {
   let len = arr.length - 1
   for (let i = 0; i < len / 2; i++) {
      ;
      [arr[i], arr[len - i]] = [arr[len - i], arr[i]]
   }
   return arr
}

let a = [1, 2, 3, 4, 5]

console.log(reverse([1, 2, 3, 4]))
console.log(reverse([1, 2, 3, 4, 5]))

function flattenDeep(arr) {
   return arr.reduce((arr, val) => Array.isArray(val) ? arr.concat(flattenDeep(val)) : arr.concat(val), [])
}

console.log(flattenDeep([1, [2, [3, [4, [5]]]]]))

function type(value) {
   return Object.prototype.toString.call(value).split(' ')[1].replace(']', '')
}

function merge(data1, data2) {
   if (type(data1) != type(data2)) {
      throw new TypeError('data1 and data2 type not unequal')
   }

   if (type(data1) == 'Array') {

      return [].concat(data1, data2)

   } else if (type(data1) == 'Object') {

      let keys = Object.keys(data1).concat(Object.keys(data2))
      let obj = {}

      keys.forEach(key => {
         if (data1.hasOwnProperty(key) && data2.hasOwnProperty(key)) {

            obj[key] = merge(data1[key], data2[key])

         } else if (!data2.hasOwnProperty(key)) {

            obj[key] = data1[key]

         } else if (!data1.hasOwnProperty(key)) {

            obj[key] = data2[key]

         }

      })

      return obj

   } else {
      return data2
   }
}

console.log(merge({
   a: 1,
   c: [1],
   d: {
      e: [3],
      f: 4
   }
}, {
   b: 2,
   c: [2],
   d: {
      e: [4],
      g: 5,
      h: function() {}
   }
}))

let arrA = [1, 2, 5, 4, 3]

/**
 * 负数 a 交换到 b 前面
 * 正数 b 交换到 a 前面
 */

arrA.sort((a, b) => {
   console.log('a::', a, ' b::', b)
   return b - a
})

arrA.sort((a, b) => {
   console.log('a::', a, ' b::', b)
   return b - a
})

console.log(arrA)

function p() {
   return new Promise((resolve, reject) => {
      reject('ho no')
   })
}

(async function() {

   await p()
   console.log('xixi')

})().catch(err => {
   console.log(err)
})

let obj = {
   a: []
}

let proxy = new Proxy(obj.a, {
   set: function(target, key, value, receiver) {
		if (key == 'length') {
			console.log('xixi')
		}
      return Reflect.set(target, key, value, receiver);
	}
})

proxy.push("123")

let string = "get-element-by-id".replace(/(\w+)/g, str => str[0].toUpperCase() + str.substr(1)).replace(/-/g, '')

console.log(string)
